import { axiosInstance } from 'src/boot/axios'
import CommonConfig from 'src/assets/js/config'

const schemaUrl = CommonConfig.schemaUrl + '/v1'
/**
 * 获取字典组数据
 * @returns {Promise<unknown>} promise
 */

export function getDictionaryGroup () {
  const url = schemaUrl + '/dictgroup_list'

  return axiosInstance.get(url).then(resp => {
    return Promise.resolve(resp.data)
  })
}

/**
 * 通过字典组id获取字典信息
 * param dicGroupCode
 * @returns {Promise<unknown>} promise
 */
export function getDictionaryList (dicGroupCode) {
  const url = schemaUrl + '/dict_by_gcode'

  let query = {
    params: {
      dicGroupCode,
      disable: false
    }
  }

  return axiosInstance.get(url, query).then(resp => {
    return Promise.resolve(resp.data)
  })
}

export function getDictList (code, params) {
  const url = `${schemaUrl}/dict_groups/${code}/dictionaries:page`

  return axiosInstance.get(url, { params }).then(resp => {
    return Promise.resolve(resp.data)
  })
}

/**
 * 启用/禁用字典项
 * param enable id
 * @returns {Promise<unknown>} promise
 */
export function changeEnable (enable, id, gcode) {
  const url = schemaUrl + '/dict_enable'

  let query = {
    params: {
      enable,
      id,
      gcode
    }
  }

  return axiosInstance.put(url, undefined, query).then(resp => {
    return Promise.resolve(resp.data)
  })
}

/**
 * 启用/禁用字典项
 * param enable id
 * @returns {Promise<unknown>} promise
 */
export function move (id1, id2, gcode) {
  const url = schemaUrl + '/dict_sort'

  let query = {
    id1,
    id2,
    gcode
  }

  return axiosInstance.put(url, query).then(resp => {
    return Promise.resolve(resp.data)
  })
}

/**
 * 删除字典项
 * @param id {number} 字典项 ID
 * @return {Promise<unknown>} promise
 */
export function deleteDictionaryList (id, gcode) {
  const url = schemaUrl + '/dict'

  let config = {
    params: {
      id,
      gcode
    }
  }

  return axiosInstance.delete(url, config).then(resp => {
    return Promise.resolve(resp)
  })
}

/**
 * 新增字典项
 * @return {Promise<unknown>} promise
 */
export function addDictionaryList (query) {
  const url = schemaUrl + '/dict'

  return axiosInstance.post(url, query).then(resp => {
    return Promise.resolve(resp)
  })
}

/**
 * 编辑字典项
 * @return {Promise<unknown>} promise
 */
export function editDictionaryList (query) {
  const url = schemaUrl + '/dict'

  let params = {
    ...query
  }

  return axiosInstance.put(url, params).then(resp => {
    return Promise.resolve(resp)
  })
}
